-- Shared / Common --


-- Set up default first team.
team.SetUp( 0, "Guest", Color( 122, 122, 122, 255 ), false ) 
team.SetUp( 1000, "Deathmatch", Color( 255, 0, 0, 255 ), false  ) 
team.SetUp( 1001, "Guest", Color( 122, 122, 122, 255 ), false  ) 
team.SetUp( 1002, "Guest", Color( 122, 122, 122, 255 ), false  )  


-- Resets client table when new team is added.
function apple_tdm_resetclienttable(data) -- This legit only is needed to reset the table
AddTeams2 = {} -- The Third Table..
TeamModels = {}	-- The Second Table
end
usermessage.Hook("apple_tdm_resetclienttable", apple_tdm_resetclienttable)


-- Creates teams on client.
net.Receive( "apple_tdm_teamcreation22", function()  
	for k, v in pairs(net.ReadTable()) do
	local exp = string.Explode(",",v.tcolor)
	team.SetUp(k, tostring(v.tname), Color(exp[1],exp[2],exp[3],255))
	util.PrecacheModel( v.tmodels )
	AddTeams2[k] = {
			id = k,
			tname = tostring(v.tname),
			tCRed = tonumber(exp[1]),
			tCGreen = tonumber(exp[2]),
			tCBlue = tonumber(exp[3]),
			tModels = tostring(v.tmodels),
			tWeapons = tostring(v.tweapons)
	}
	TeamModels[k] = {
		id = k,
		tmodels = tostring(v.tmodels),
		tweapons = tostring(v.tweapons)
		}
	end
end)


-- Create Teams From Database (Server Side)
function CreateTeams()
local CreateTeamInfoAll = sql.Query( "SELECT * FROM apple_tdm_teamcreation22" )
	if CreateTeamInfoAll == nil then return end -- Just in case something goes wrong, we don't want teams being created incorrectly, so it will just end here
	AddTeams = {} -- The Table
	TeamModels2 = {} -- The Table
	TeamScore = {}
	for k, v in pairs( CreateTeamInfoAll ) do
		
		TeamScore[tonumber(v['ID'])] = team.GetScore(tonumber(v['ID']))
		
		team.SetUp(tonumber(v['ID']), tostring(v['Name']), Color(tonumber(v['CRed']),tonumber(v['CGreen']),tonumber(v['CBlue']),255))  -- This creates the team server side
		team.SetScore(tonumber(v['ID']), 0)
		util.PrecacheModel( tostring(v['Models']) ) -- I do not really know what this is, I just saw this in base gamemode
		
		
		AddTeams[k] = { -- This is the table that is added so it will transfer this to client side
			tid = tonumber(v['ID']),
			tname = tostring(v['Name']),
			tcolor = tonumber(v['CRed'])..","..tonumber(v['CGreen'])..","..tonumber(v['CBlue']),
			tmodels = tostring(v['Models']),
			tweapons = tostring(v['Weapons'])
		}
		TeamModels2[k] = {
			id = tonumber(v['ID']),
			tmodels = tostring(v['Models']),
			tweapons = tostring(v['Weapons'])
		}
	end
		
	net.Start( "apple_tdm_teamcreation22" )
		net.WriteTable(AddTeams)
	net.Broadcast()
		
		
	for k,v in pairs(player.GetAll()) do 
		umsg.Start("apple_tdm_updatelist",v) -- Resets the menu player list if it is open for anyone
		umsg.End()
	end
	for k, v in pairs(TeamScore) do
		team.SetScore(k, v)
	end
	
--	CreateSpawns()
end


-- Sends Server to Client Team Data
function SendTeams2Client( ply ) 
if AddTeams == nil then return end
		net.Start( "apple_tdm_teamcreation22" )
			net.WriteTable(AddTeams)
		net.Broadcast()
end
hook.Add( "PlayerInitialSpawn", "SendTeams2Client", SendTeams2Client )
